<?php
/**
 * 资质-营业执照审核
 * Created by PhpStorm.
 * User: 50223
 * Date: 18.8.7
 * Time: 17:08
 */
namespace App\Http\Controllers\Aptitude;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
class LicenseController extends Controller
{
        public function license (Request $request){

//            审核系统中只查询审核通过的
            $licenseList =  DB::table('cust_material') ->orderBy('id','desc')->where('license_status','=',1)->paginate(20);

//            获取所有的历史记录 type=1
            $historyAll = DB::table('history')->where('type','=',1)->orderBy('id','desc')->paginate(200);


            return view('aptitude/license',[
                'licenseList'=>$licenseList,
                 'historyAll'=>$historyAll
            ]);

        }

        public function license_info(Request $request){

            if($request->isMethod('POST')){
                $userid = $_POST['userid'];
                $license_info = DB::table('cust_material')->where('id','=',$userid)->first();
                return array('license_info'=>$license_info);

            }
        }

        public function license_update(Request $request){

            if($request->isMethod('POST')){

//                图片上传
                $creditma = $_POST['creditma'];
                $time = $_POST['time'];
                $cusname = $_POST['cusname'];
                $userid = $_POST['userid'];
                // 验证的正则
                $check_time  = " /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/";
                if(trim($time)=='永久'){
                    $time = 1;
                }else{
                    if (!preg_match($check_time, $time)) {
                        return array('status' => 0, 'msg' => '日期格式不正确，正确格式为：2018-08-08');
                        exit;
                    }else{
                        $time = strtotime($time);
                    }

                }

                if($userid){
//                    执行变更与历史操作记录

//                    注意此userId为大写
                    $userId = $request->session()->get('userId');

                    if(!$userId){

                       return  array('status'=>2,'msg'=>'登录过期，请重新登录');

                    }else{

                        $res = DB::table('cust_material')->where('id','=',$userid)->update([
                           'cust_name'=>$cusname,'license_validity'=>$time,'credit_code'=>$creditma
                        ]);

                        $username = DB::table('users')->where('id','=',$userId)->value('username');

                        $history = DB::table('history')->insert([
                            'userid'=>$userId,
                            'username'=>$username,
                            'created_time'=>date('Y-m-d H:i:s',time()),
                            'description'=>'营业执照变更',
                            'tag'=>'营业执照变更',
                            'type'=>1
                        ]);
                    }
                    return array('status'=>1,'msg'=>'营业执照变更成功');

                }else{

                    return array('status'=>0,'msg'=>'营业执照变更失败');exit;
                }


            }
        }

        public function correction(Request $request){

        if($request->isMethod('POST')){

            $creditma = $_POST['creditma'];
            $time = $_POST['time'];
            $cusname = $_POST['cusname'];
            $userid = $_POST['userid'];
            // 验证的正则
            $check_time  = " /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/";
            if(trim($time)=='永久'){
                $time = 1;
            }else{
                if (!preg_match($check_time, $time)) {
                    return array('status' => 0, 'msg' => '日期格式不正确，正确格式为：2018-08-08');
                    exit;
                }else{
                    $time = strtotime($time);
                }

            }

            if($userid){
                $userId = $request->session()->get('userId');

                if(!$userId){

                    return  array('status'=>2,'msg'=>'登录过期，请重新登录');exit;

                }else{
                    $username = DB::table('users')->where('id','=',$userId)->value('username');

                    $res = DB::table('cust_material')->where('id','=',$userid)->update([
                        'cust_name'=>$cusname,'license_validity'=>$time,'credit_code'=>$creditma
                    ]);

                    $history = DB::table('history')->insert([
                        'userid'=>$userId,
                        'username'=>$username,
                        'created_time'=>date('Y-m-d H:i:s',time()),
                        'description'=>'营业执照纠错',
                        'tag'=>'营业执照纠错',
                        'type'=>1
                    ]);
                }

                if($res&&$history){

                    return array('status'=>1,'msg'=>'营业执照纠错成功');
                }else{
                    return array('status'=>0,'msg'=>'营业执照纠错失败');exit;
                }
            }else{

                return array('status'=>0,'msg'=>'营业执照纠错失败');exit;
            }


        }
    }

    public function cust_id_screen(Request $request){

        if($request->isMethod('POST')){
            $cust_id = trim($_POST['cust_id']);
            $dataList = DB::table('cust_material')->where('cust_id','=',$cust_id)->paginate(20);
            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');

            }

        }else{

            return array('status'=>0,'msg'=>'操作失败');exit;
        }
    }

    public function cust_name_screen(Request $request){
        if($request->isMethod('POST')){
            $cust_name = trim($_POST['cust_name']);
            $dataList = DB::table('cust_material')->where('cust_name','=',$cust_name)->paginate(20);
            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');

            }

        }else{

            return array('status'=>0,'msg'=>'操作失败');exit;
        }
    }

    public function credit_code_screen(Request $request){
        if($request->isMethod('POST')){
            $credit_code = trim($_POST['credit_code']);
            $dataList = DB::table('cust_material')->where('credit_code','=',$credit_code)->paginate(20);
            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');

            }

        }else{

            return array('status'=>0,'msg'=>'操作失败');exit;
        }
    }

    public function legal_name_screen(Request $request){
        if($request->isMethod('POST')){
            $legal_name = trim($_POST['legal_name']);
            $dataList = DB::table('cust_material')->where('legal_name','=',$legal_name)->paginate(20);
            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');

            }

        }else{

            return array('status'=>0,'msg'=>'操作失败');exit;
        }
    }

    public function time_screen(Request $request){
        if($request->isMethod('POST')){
            $beginTime = strtotime(trim($_POST['beginTime']));
            $endTime = strtotime(trim($_POST['endTime']));

            $dataList = DB::table('cust_material')->whereBetween('license_validity',[$beginTime,$endTime])->orWhere('license_validity','=',1)  ->paginate(20);
            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');

            }

        }else{

            return array('status'=>0,'msg'=>'操作失败');exit;
        }
    }

    public function upload(Request $request){
        $pic = $request->file('legal_card');
        $cust_id = $_POST['cust_id'];

        if($pic){
            $name = $pic->getClientOriginalName();//得到图片名；
            $path = date('Y-m');
            $fileName = rand(1,999).'_'.$name;//生成新的的文件名
            Storage::disk('material')->makeDirectory($path);
            $filePath = $path.'/'.$fileName;
            Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));


            $res = DB::table('cust_material')->where('id','=',$cust_id)->update([
                'license'=>$filePath,
            ]);

            if($res){
                return array('status'=>1,'msg'=>'上传成功');
            }else{
                return array('status'=>0,'msg'=>'上传失败');
            }

        }


}


        public function download(Request $request){

            if($request->isMethod('POST')){

                $allId= $_POST['allId'];
                for ($i=0;$i<count($allId);$i++){
                    $url[] = '/upload/material/'.DB::table('cust_material')->where('id',$allId[$i])->value('license');
                    $name[] = DB::table('cust_material')->where('id',$allId[$i])->value('cust_name');
                }


                return array('url'=>$url,'name'=>$name);
            }

        }



}